package 第7周_二分查找法.二分查找法的变种;

import java.util.Arrays;

public class Solution2 {
    public int minEatingSpeed(int[] piles, int h) {
        //左边界为0， 右边界为最大的一堆香蕉
        int l = 1, r = Arrays.stream(piles).max().getAsInt();
        while(l < r){
            int mid = l + (r - l) / 2;
            if(eatingTime(piles, mid) <= h){
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
        return l;
    }

    private int eatingTime(int[] piles, int k){
        int res = 0;
        for(int pile : piles){
            res += pile / k + (pile % k > 0 ? 1 : 0);
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = {3,6,7,11};
        System.out.println((new Solution2()).minEatingSpeed(nums, 8));
    }
}
